package com.gdk.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class XVDateUtils {
	
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	/**
	 * @see 获取当前时间毫秒
	 * @author xuwei
	 * @return 当前时间毫秒int类型
	 * */
	public static int getCurrenTimeMillis() {
		int millis = (int) (System.currentTimeMillis()/1000);
		return millis;
	}
	
	/**
	 * @see 将毫秒时间格式化    -28800 --> 1970-01-01 00:00:00
	 * @author xuwei
	 * @param int类型时间毫秒
	 * @return 格式化时间字符串
	 * */
	public static String MillisFormat(int millis) {
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		int pre_begin_time = millis;
		long long_begin_time = pre_begin_time *1000L;
		//时间格式
		Date date = new Date(long_begin_time);
		String formatTime = dateFormat.format(date);
		return formatTime;	
	}
	
	/**
	 * @desc 将 yyyy-MM-dd HH:mm:ss 时间转换为int秒
	 * //例将2019-11-15 00:00:00 - 2019-12-21 00:00:00 转为   开始时间  13212114541  结束时间 1511211212
	 * @author xuwei
	 * @param String yyyy-MM-dd HH:mm:ss 格式分割
	 * @return Map f_time e_time
	 * */
	public static Map<String,Object> dateCutMillis(String date) {
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		Map<String,Object> dateMap = new HashMap<String,Object>();
		
		String pre_f_time = date.substring(0, date.lastIndexOf(" - "));
		String pre_e_time = date.substring(date.lastIndexOf(" - ") +3,date.length() );
		System.out.println("原始时间 : "+date);
		
		try {
			long long_first_hd = dateFormat.parse(pre_f_time).getTime();
			int f_time = (int) (long_first_hd / 1000);
			long long_last_hd = dateFormat.parse(pre_e_time).getTime();
			int e_time = (int) (long_last_hd / 1000);
			dateMap.put("f_time", f_time);
			dateMap.put("e_time", e_time);
		} catch (ParseException e) {
			dateMap.put("f_time", "-28800"); //1970-01-01 00:00:00
			dateMap.put("e_time", "-28800");
		}
		System.out.println("原始时间转化后 : "+dateMap);
		
		return dateMap;	
	}
	public static int stringDateToMillis(String date) {
		long long_last_hd;
		int millis = 0;
		try {
			long_last_hd = dateFormat.parse(date).getTime();
			millis = (int) (long_last_hd / 1000);
		} catch (ParseException e) {
			
		}
		
		return millis;
		
	}
	
}
